/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.Customer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SIX-14
 */
public class ThreadSocket extends Thread implements Serializable{

    public ThreadSocket(Socket socket) throws IOException{
        this.socket = socket;
        try {
            salidaObjetos = new ObjectOutputStream(this.socket.getOutputStream());
            salidaObjetos.flush();
            entradaObjetos = new ObjectInputStream(this.socket.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.start();
    }
Socket socket;
private ObjectInputStream entradaObjetos;
private ObjectOutputStream salidaObjetos;
private String[] process=null;
private Monitor monitor=null;
private int option=0;

    public int getOption() {
        return option;
    }

    public void setOption(int option) {
        this.option = option;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public String[] getProcess() {
        return process;
    }

    public void setProcess(String[] process) {
        this.process = process;
    }

    public synchronized void run(){
        while(true){
            if(option==0){
                try {
                    this.sleep(1500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);
                }
                

            }else if(option==1){
                beginmonitor(getProcess());
                option=0;
            }else if(option==2){
                monitor=receiveMonitor();
                option=0;
            }else if(option==3){
                monitor=null;
                option=0;
            }
        }
    }

    public void close(){
        try {
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getInetAddress(){
        return socket.getInetAddress().toString();
    }

    public void beginmonitor(String[] process){
        String[] sendProcess= process;
        //System.out.println(process[0]);
            try {
                salidaObjetos.writeObject(sendProcess);
                salidaObjetos.flush();
            } catch (IOException ex) {
                Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    public Monitor receiveMonitor(){
        Monitor monitor=null;
        try {
            Object aux = null;
            try {
                salidaObjetos.writeObject("Enviar");
                salidaObjetos.flush();
            } catch (IOException ex) {
                Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            aux = entradaObjetos.readObject();
            if (aux != null) {
                System.out.println(aux);
                if (aux instanceof Monitor) {
                    monitor= (Monitor)aux;
                }
            }
        } catch (IOException ex) {
                Logger.getLogger(ThreadSocket.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            System.out.println("Error de clase");
        }
        return monitor;
    }

}
